<?
/**
 * VoiceOne Settings/Extensions/Voicemail
 *
 * Frontend to provide access to the extensions system in voiceone.
 * @package voiceone
 * @subpackage  settings_extensions
 */
 
  include ("../../../../admin/config/cfg.php");
  global $cwlang;
  require_once("$absBasePath/lib/xajax/xajax.inc.php"); 
  
  //================ XAJAX ================
  function processFormData($formData){
    global $asterisk,$cwlang,$relBasePath;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    $confirmBeforeSubmit="";
    //extract form params
    $voicemail=$formData["voicemail"];
    $id=$voicemail["id"];
    $mailbox=$voicemail["mailbox"];
    $oldmailbox=$voicemail["oldmailbox"];
    $vpassword=$voicemail["password"];
    $vconfirm=$voicemail["confirm"];
    $imgErr="<img src=\"".$relBasePath."/public/img/shutdown.png\" alt=\"error\">";
    //retrieve all mailbox etensions
    $ret=$asterisk->getVoiceMail(0);
    if(is_array($ret)){
      foreach($ret as $m){
        if($m["id"]!=$id) $mailboxArr[]=$m["mailbox"];
      }
    }
    // Check mailbox ((no null)&&(numeric)&&(unique))
    $mailboxErr=false;
    if($mailbox=="") { 
      //mailbox cannot be null
      $errOut[]= T_("Mailbox cannot be null");
      $objResponse->addAssign("mailboxErr","innerHTML",$imgErr);
      $mailboxErr=true;
    }else{
      //mailbox must be numeric
      for($i=0; $i<strlen($mailbox); $i++){ 
        $ch=ord($mailbox[$i]);
        if(!((($ch>47)&&($ch<58)))){
          $errOut[]= T_("Mailbox allows only numeric characters. Please change it");
          $objResponse->addAssign("mailboxErr","innerHTML",$imgErr);
          $mailboxErr=true;
          break;
        }
      }
      //mailbox must be unique
      if((is_array($mailboxArr))&&(in_array($mailbox,$mailboxArr))) {
        $errOut[]= T_("Mailbox already exists. Please change it");
        $objResponse->addAssign("mailboxErr","innerHTML",$imgErr);
        $mailboxErr=true;
      }    
    }
    if($mailboxErr===false) $objResponse->addAssign("mailboxErr","innerHTML","");
    // Check Voicemail Password ((==confirm)&&(numeric))
    $vpasswordErr=false;
    if($vpassword!=$vconfirm){
      //voicemail password must be equal to confirm
      $errOut[]=T_("Voicemail password and confirmation password do not match");
      $objResponse->addAssign("vpasswordErr","innerHTML",$imgErr);
      $objResponse->addAssign("vconfirmErr","innerHTML",$imgErr);
      $vpasswordErr=true;
    }else{
      //voicemail password must be numeric
      for($i=0; $i<strlen($vpassword); $i++){ 
        $ch=ord($vpassword[$i]);
        if(!((($ch>47)&&($ch<58)))){
          $errOut[]=T_("Voicemail password allows only numeric characters. Please change it");
          $objResponse->addAssign("vpasswordErr","innerHTML",$imgErr);
          $vpasswordErr=true;
          break;
        }
      }
    }
    if($vpasswordErr===false){
      $objResponse->addAssign("vpasswordErr","innerHTML","");
      $objResponse->addAssign("vconfirmErr","innerHTML","");
    }
    //Check if change mailbox
    if(($oldmailbox!="")&&($oldmailbox!=$mailbox)){
      $ret=$asterisk->gelClientFromExtension($oldmailbox);
      if(is_array($ret)){
        $confirmBeforeSubmit=T_("If you continue the mailbox will be turned off for the extension it is linked to.\\nAre you sure?");
      }
    }
    // Process Form 
    if(isset($errOut)){
      $error="<div class=\"AlertBad\"><ul>";
      foreach($errOut as $err) $error.="<li>".$err."</li>";
      $error.="</ul></div>";
      $objResponse->addAssign("alert","innerHTML",$error);
      $objResponse->addAssign("loading","innerHTML","");
    }else{
      $objResponse->addAssign("loading","innerHTML",""); 
      if($confirmBeforeSubmit!="") $script=" if(confirm('".$confirmBeforeSubmit."')) document.getElementById('mainform').submit(); "; else $script=" document.getElementById('mainform').submit(); ";
      $objResponse->addScript($script);
    }
    // Return the xajaxResponse object
    return $objResponse->getXML();  
  }
  $xajax = new xajax();
  $xajax->registerFunction("processFormData");
  $xajax->processRequests();
  //================ POST & GET =================
  if((count($_POST)>0)&&(isset($_POST["voicemail"]))){
    //create structure
    $voicemail=$_POST["voicemail"];
    $entry=Array(
        "mailbox"   => $voicemail["mailbox"],
        "password"  => $voicemail["password"],
        "fullname"  => $voicemail["name"],
        "email"     => $voicemail["email"]
    );
    if((isset($voicemail["id"]))&&($voicemail["id"]!="")) $entry["id"]=$voicemail["id"];
    if((isset($voicemail["attach"]))&&($voicemail["attach"]==1)) $entry["attach"]=1;
    if((isset($voicemail["saycid"]))&&($voicemail["saycid"]==1)) $entry["saycid"]=1;
    if((isset($voicemail["hidefromdir"]))&&($voicemail["hidefromdir"]==1)) $entry["hidefromdir"]=1;
    //create mailbox
    $ret=$asterisk->putVoiceMail($entry);
    if ($ret>0) $alert="<div class=\"AlertGood\">".T_("Mailbox has been successfully edited")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("Mailbox has NOT been edited")."</div>";
    //go to main page
    goToMainPage($alert,"../");
  }
  //================ PAGE LOGIC ================
  //get mailboxes
  if((isset($_GET["id"]))&&($_GET["id"]!="")){
    $ret=$asterisk->getVoiceMail($_GET["id"]);
    if(is_array($ret)){
      $ret=$ret[0];
      $id=$ret["id"];
      $mailbox=$ret["mailbox"];
      $name=$ret["fullname"];
      $email=$ret["email"];
      $password=$ret["password"];
      $attach=$ret["attach"];
      $saycid=$ret["saycid"];
      $hidefromdir=$ret["hidefromdir"];
    }
    $title=T_("Edit Voicemail")." \"".$mailbox."\"";
  }else{
    $title=T_("Edit Voicemail");
  }
  //================ MAIN CONTENTS ================
  ob_start();
  $xajax->printJavascript("../../../../admin/lib/xajax/"); 
  ?>
    <div class="SectionHeader">
      <div id="loading" style="float: right;"></div>
      <h1><?=$title?></h1>
      <h3><a href="../" class="admin" ><?=T_("Return without save")?></a></h3>
    </div>  
    <div id="alert"></div>
    <div id="basic_form">
      <form id="mainform" action="<?=$PHP_SELF?>" method="post">
        <!-- Voicemail -->
        <h2><?=T_("VoiceMail")?></h2>
        <p class="blockintro"><?=T_("Insert here the voicemail information")?></p>
        <div class="block">
          <dl>
            <dt class="required"><label for="voicemail[mailbox]"><?=T_("Mailbox")?>:</label></dt>
            <dd><input type="text" size="25" id="voicemail[mailbox]" name="voicemail[mailbox]" value="<?=$mailbox?>" /><span id="mailboxErr"></span> <?=T_("Number of the Mailbox")?></dd>
            <dt class="required"><label for="voicemail[name]"><?=T_("Name")?>:</label></dt>
            <dd><input type="text" size="25" id="voicemail[name]" name="voicemail[name]" value="<?=$name?>" /> <?=T_("VoiceMail User Name")?></dd>
            <dt class="required"><label for="voicemail[email]"><?=T_("Email")?>:</label></dt>
            <dd><input type="text" size="25" id="voicemail[email]" name="voicemail[email]" value="<?=$email?>" /> <?=T_("Email address (leave blank for no notification)")?></dd>
            <dt class="required"><label for="voicemail[password]"><?=T_("Password")?>:</label></dt>
            <dd><input type="password" size="25" id="voicemail[password]" name="voicemail[password]" value="<?=$password?>" /><span id="vpasswordErr"></span> <?=T_("Set the password to hear voicemail")?></dd>
            <dt class="required"><label for="voicemail[confirm]"><?=T_("Confirm")?>:</label></dt>
            <dd><input type="password" size="25" id="voicemail[confirm]" name="voicemail[confirm]" value="<?=$password?>" /><span id="vconfirmErr"></span> <?=T_("Confirm the password to hear voicemail")?></dd>
            <dt class="required"><label for="voicemail[attach]"><?=T_("Attach")?>:</label></dt>
            <dd><input type="checkbox" id="voicemail[attach]" name="voicemail[attach]" value="1" <?if ($attach==1) echo "checked=\"checked\""; ?> />
            <?=T_("If enabled, the audio message will be sent as attachment to your email")?> </dd>
            <dt class="required"><label for="voicemail[saycid]"><?=T_("Say CID")?>:</label></dt>
            <dd><input type="checkbox" id="voicemail[saycid]" name="voicemail[saycid]" value="1" <?if ($saycid==1) echo "checked=\"checked\""; ?> />
            <?=T_("If enabled, the Caller ID will be included")?></dd>
            <dt class="required"><label for="voicemail[hidefromdir]"><?=T_("Hide")?>:</label></dt>
            <dd><input type="checkbox" id="voicemail[hidefromdir]" name="voicemail[hidefromdir]" value="1"  <?if ($hidefromdir==1) echo "checked=\"checked\""; ?> />
            <?=T_("If enabled, you will be hidden from the directory")?></dd>
          </dl>
        </div>
        <!-- Buttons -->
        <div class="action">
          <?if ($_GET["id"]!="") {?> <input type="hidden" id="voicemail[id]" name="voicemail[id]" value="<?=$id?>" /><input type="hidden" id="voicemail[oldmailbox]" name="voicemail[oldmailbox]" value="<?=$mailbox?>" /><?}?>
          <a href="#"><img src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-cancel.gif" alt="Cancel" onclick="location.href='../'; return false;"/></a>
          <a href="#"><img src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-save.gif" alt="Save" onclick=" document.getElementById('alert').innerHTML=''; xajax_processFormData(xajax.getFormValues('mainform')); "/></a>
        </div>
      </form>
    </div>
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
    <div class="Sidebar">
      <?$sel="voicemail";include("../../sidebar.php");?>
      <h1><?=T_("Informations")?></h1>
    </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================
  $smarty->assign("section", "extensions");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('settings.tpl');
?>

